# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, String, text
from sqlalchemy.dialects.mysql import TINYINT
from sqlalchemy.ext.declarative import declarative_base
from pydantic import BaseModel, field_validator
from datetime import datetime

Base = declarative_base()
metadata = Base.metadata # type: ignore


class User(Base):
    __tablename__ = 'users'
    __table_args__ = {'comment': '员工账号表'}

    userAccountId = Column(String(256), primary_key=True, comment='用户账号 id')
    userNickname = Column(String(256), comment='昵称')
    userAccount = Column(String(256), nullable=False, unique=True, comment='用户账号')
    userAvatar = Column(String(1024), comment='用户头像')
    roleuserIdList = Column(String(256), nullable=False, server_default=text("'[]'"), comment='格式: [roleId, roleId, ...]')
    userPassword = Column(String(512), nullable=False, comment='账号的密码')
    createId = Column(String(256), nullable=False, comment='创建者账号 id')
    updateId = Column(String(256), nullable=False, comment='修改者账号 id')
    createTime = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"), comment='创建时间')
    updateTime = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"), comment='更新时间')
    isDelete = Column(TINYINT, nullable=False, server_default=text("'0'"), comment='是否删除')

class UserItem(BaseModel):
    userAccountId: str
    userNickname: str
    userAccount: str
    userAvatar: str
    roleuserIdList: str
    userPassword: str
    createId: str
    updateId: str
    createTime: str
    updateTime: str
    isDelete: int
    model_config = {
        'from_attributes': True,
        }
    
    @field_validator('createTime', 'updateTime', mode='before') 
    @classmethod 
    def convert_datetime_to_str(cls, v: datetime) -> str: 
        return v.isoformat()
    
    @field_validator('userAvatar', mode='before')
    @classmethod
    def convert_none_to_empty_str(cls, v: str) -> str:
        if v is None:
            return ''
        return v
